package com.bkgy.modular.tbdsjwarnruleedit.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bkgy.modular.alarm.entity.TbDsjWarnRuleEdit1;
import com.bkgy.modular.tbdsjwarnruleedit.dao.TbDsjWarnRuleEditDao;
import com.bkgy.modular.tbdsjwarnruleedit.entity.JTbDsjWarnRuleEdit;
import com.bkgy.modular.tbdsjwarnruleedit.entity.TbDsjWarnRuleEdit;
import com.bkgy.modular.tbdsjwarnruleedit.service.TbDsjWarnRuleEditService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.CodiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* 报警规则编辑表接口具体实现类
* @author  生成器生成
* @date 2022-08-01 15:16:08
*/
@Service
public class TbDsjWarnRuleEditServiceImpl implements TbDsjWarnRuleEditService {
	
	@Autowired
	private TbDsjWarnRuleEditDao tbDsjWarnRuleEditDao;
	
	/**
     * 添加记录
     * @param data json字符串
     * @param userId 用户id
     * @param sname 用户姓名
     */
	public ResultData insertDataTbDsjWarnRuleEdit(String data,Long userId,String sname){
		try{
			JTbDsjWarnRuleEdit jtbDsjWarnRuleEdit = JSON.parseObject(data,JTbDsjWarnRuleEdit.class);
    		TbDsjWarnRuleEdit tbDsjWarnRuleEdit = jtbDsjWarnRuleEdit.getTbDsjWarnRuleEdit();
    		CodiUtil.newRecord(userId,sname,tbDsjWarnRuleEdit);
            tbDsjWarnRuleEditDao.insertDataTbDsjWarnRuleEdit(tbDsjWarnRuleEdit);
            return ResultData.ResultDataSuccessSelf("添加数据成功", null);
    	}catch(Exception e){
    		e.printStackTrace();
    		return ResultData.ResultDataFaultSelf("添加失败，错误信息为" + e.getMessage(), null); 
    	}
	};
	
	/**
	 * 根据主键删除对象
	 * @param indocno  对象主键
	 * */
	public ResultData deleteDataTbDsjWarnRuleEditOne(Long indocno){
		try {
            tbDsjWarnRuleEditDao.deleteDataTbDsjWarnRuleEditOne(indocno);
             return ResultData.ResultDataSuccessSelf("删除数据成功", null);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null); 
        }
	};
	
	/**
     * 根据主键字符串删除多个对象
     * @param str_id 对象主键字符串
     */
    public ResultData deleteDataTbDsjWarnRuleEditMany(String str_id) {
        try {
        	String sql = "delete tb_dsj_warn_rule_edit where indocno in(" + str_id +")";
            tbDsjWarnRuleEditDao.deleteDataTbDsjWarnRuleEditMany(sql);
            return ResultData.ResultDataSuccessSelf("删除数据数据成功", null);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null); 
        }
    }
	
	/**
     * 修改记录
     * @param userId 用户id
     * @param sname 用户姓名
     * @param data sysUser 对象实体
     */
	public ResultData updateDataTbDsjWarnRuleEdit(String data,Long userId,String sname){
		try {
			JTbDsjWarnRuleEdit jtbDsjWarnRuleEdit = JSON.parseObject(data,JTbDsjWarnRuleEdit.class);
    		TbDsjWarnRuleEdit tbDsjWarnRuleEdit = jtbDsjWarnRuleEdit.getTbDsjWarnRuleEdit();
        	CodiUtil.editRecord(userId,sname,tbDsjWarnRuleEdit);
            tbDsjWarnRuleEditDao.updateDataTbDsjWarnRuleEdit(tbDsjWarnRuleEdit);
            return ResultData.ResultDataSuccessSelf("修改成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("修改失败,失败信息为" + e.getMessage(), null); 
        }
	};
	
	/**
     * 分页查看记录
     * @param data 分页参数字符串
     */
    public ResultData findDataTbDsjWarnRuleEditByPage(String data) {
        try {
        	JTbDsjWarnRuleEdit jtbDsjWarnRuleEdit = JSON.parseObject(data, JTbDsjWarnRuleEdit.class); 
        	JSONObject jsonObject  = null;
        	Integer pageIndex = jtbDsjWarnRuleEdit.getPageIndex();
        	Integer pageSize = jtbDsjWarnRuleEdit.getPageSize();
        	
        	if(null == pageIndex || null == pageSize){
        		return ResultData.ResultDataFaultSelf("分页参数没有传", null); 
        	}
        	
        	if(null!=jtbDsjWarnRuleEdit.getCondition()){
    			jsonObject  = JSON.parseObject(jtbDsjWarnRuleEdit.getCondition().toString());
    		}

			String warning_l1_variable_name = null;
			if (!StringUtils.isEmpty(jsonObject.get("warning_l1_variable_name"))) {
				warning_l1_variable_name = jsonObject.get("warning_l1_variable_name").toString();
			}
			String process_rule_variable = null;
			if (!StringUtils.isEmpty(jsonObject.get("process_rule_variable"))) {
				process_rule_variable = jsonObject.get("process_rule_variable").toString();
			}
    
    		List<TbDsjWarnRuleEdit1> list = tbDsjWarnRuleEditDao.findDataTbDsjWarnRuleEditByPage((pageIndex-1)*pageSize, pageIndex*pageSize,warning_l1_variable_name,process_rule_variable);
    		Integer count = tbDsjWarnRuleEditDao.findDataTbDsjWarnRuleEditByPageSize(warning_l1_variable_name,process_rule_variable);
    		return ResultData.ResultDataSuccess(list, count);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null); 
        }
    }
    
    /**
     * 根据主键查询单条记录
     * @param data 分页参数字符串
     */
    public ResultData findDataTbDsjWarnRuleEditByIndocno(String data) {
        try {
        	JTbDsjWarnRuleEdit jtbDsjWarnRuleEdit = JSON.parseObject(data, JTbDsjWarnRuleEdit.class); 
        	Long indocno = jtbDsjWarnRuleEdit.getIndocno();
        	
    		TbDsjWarnRuleEdit tbDsjWarnRuleEdit = tbDsjWarnRuleEditDao.findDataTbDsjWarnRuleEditByIndocno(indocno);
    		return ResultData.ResultDataSuccess(tbDsjWarnRuleEdit);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null); 
        }
    }
	
	/**
	 * 查看记录
	 * @return list 对象集合返回
	 * */
	public List<TbDsjWarnRuleEdit> findDataTbDsjWarnRuleEdit(){
		List<TbDsjWarnRuleEdit> list = tbDsjWarnRuleEditDao.findDataTbDsjWarnRuleEdit();
		return list;
	};
}
